package servlets;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import webservices.helper.Helper;
import control.ControlService;

/**
 * Servlet implementation class CourseManageServlet
 */
public class CourseDetailServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request,response);
	}
	
	private void processRequest(HttpServletRequest request, HttpServletResponse response){
		try{
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e){}
		
		String requestXML = Helper.paramStr("courseID", Helper.getParam(request, "courseID"));
		
		String responseXML = new ControlService().Call("CourseDetail", requestXML);
		
		try {
			NodeList responseList = Helper.getResponse(responseXML);
			
			String messageJson = "[";
			String slideJson = "[";
			
			for (int i = 0; i < responseList.getLength(); i++) {
				Element r = (Element)responseList.item(i);
				String name = r.getAttribute("name");

				if ("message".equals(name)){
					messageJson += "{messageID: '" + r.getElementsByTagName("messageID").item(0).getTextContent() + "',";
					messageJson += "subject: '" + r.getElementsByTagName("subject").item(0).getTextContent() + "',";
					messageJson += "content: '" + r.getElementsByTagName("content").item(0).getTextContent() + "',";
					messageJson += "userName: '" + r.getElementsByTagName("userName").item(0).getTextContent() + "',";
					messageJson += "createTime: '" + r.getElementsByTagName("createTime").item(0).getTextContent() + "'},";
				} else if ("slide".equals(name)){
					if ( i > 1 ){
						slideJson += ",";
					}
					slideJson += "{slideID: '" + r.getElementsByTagName("slideID").item(0).getTextContent() + "',";
					slideJson += "name: '" + r.getElementsByTagName("name").item(0).getTextContent() + "',";
					slideJson += "description: '" + r.getElementsByTagName("description").item(0).getTextContent() + "',";
					slideJson += "link: '" + r.getElementsByTagName("link").item(0).getTextContent() + "'}";
				}
			}
			messageJson += "]";
			slideJson += "]";
			response.setCharacterEncoding("utf-8");
			response.getWriter().write("{slides:"+slideJson + ", messages:" + messageJson + "}");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
